#include "..\examples\src\shared\gltools.h"

const float GL_PI = 3.1415f;

float xRot = 0.0f;
float yRot = 0.0f;

void RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT);

	GLfloat sizeRange[2];
	glGetFloatv(GL_POINT_SIZE_RANGE, sizeRange);
	GLfloat pSize = sizeRange[0];
	GLfloat step;
	glGetFloatv(GL_POINT_SIZE_GRANULARITY, &step);
	GLfloat x, y, z, angle;

	glPushMatrix();
	glRotatef(xRot, 1.0f, 0.0f, 0.0f);
	glRotatef(yRot, 0.0f, 1.0f, 0.0f);

	z = -70;
	for (angle = 0.0f; angle < (3.0f * GL_PI * 2.0f); angle += 0.1)
	{
		glPointSize(pSize);
		x = 50.0f * sin(angle);
		y = 50.0f * cos(angle);

		glBegin(GL_POINTS);
		
		glVertex3f(x, y, z);

		glEnd();

		pSize += step;
		z += 0.5f;
	}

	glPopMatrix();


	glutSwapBuffers();
}

void ChangeSize(GLsizei w, GLsizei h)
{
	GLfloat nRange = 100.0f;

	if (h == 0)
		h = 1;
	GLfloat aspectRatio = static_cast<GLfloat>(w) / static_cast<GLfloat>(h);

	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	if (w <= h)
	{
		glOrtho(-nRange, nRange, -nRange / aspectRatio, nRange / aspectRatio, -nRange, nRange);
	}
	else
	{
		glOrtho(-nRange * aspectRatio, nRange * aspectRatio, -nRange, nRange, -nRange, nRange);
	}

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void SpecialKeys(int key, int x, int y)
{
	if (key == GLUT_KEY_UP)
	{
		xRot -= 5.0f;
	}
	else if (key == GLUT_KEY_DOWN)
	{
		xRot += 5.0f;
	}
	else if (key == GLUT_KEY_LEFT)
	{
		yRot -= 5.0f;
	}
	else if (key == GLUT_KEY_RIGHT)
	{
		yRot += 5.0f;
	}

	if (xRot > 356.0f)
	{
		xRot = 0.0f;
	}

	if (xRot < -1.0f)
	{
		xRot = 355.0f;
	}

	if (yRot > 356.0f)
	{
		yRot = 0.0f;
	}

	if (yRot < -1.0f)
	{
		yRot = 355.0f;
	}

	glutPostRedisplay();
}

void SetupRC()
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glColor3f(0.0f, 1.0f, 0.0f);
}

int main(int argc, char * argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutCreateWindow("pointsz");
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(ChangeSize);
	glutSpecialFunc(SpecialKeys);

	SetupRC();
	glutMainLoop();

	return 0;
}